package q_javadb;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JLabel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.InetAddress;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Properties;

import javax.swing.JTextField;

public class RemoteConnection extends JInternalFrame {
	String driver = "org.apache.derby.jdbc.ClientDriver";
	//String url = "jdbc:derby://192.168.10.110:1527/bookrent;create=true";
	String url;
	Connection conn;
	
	private JPanel panel;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JScrollPane scrollPane;
	private JTextArea textArea;
	private JLabel lblNewLabel;
	private JTextField server;
	private JLabel lblNewLabel_1;

	public RemoteConnection() {
		super("Remote Connection Test", true, true, true, true);
		setBounds(100, 100, 383, 297);
		setDefaultCloseOperation(JInternalFrame.HIDE_ON_CLOSE);
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getScrollPane(), BorderLayout.CENTER);
		getContentPane().add(getLblNewLabel(), BorderLayout.SOUTH);
		try{
			InetAddress ia = InetAddress.getLocalHost();
			server.setText(ia.getHostAddress());
			
			
		}catch(Exception ex){
			
		}
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.add(getLblNewLabel_1());
			panel.add(getServer());
			panel.add(getBtnNewButton());
			panel.add(getBtnNewButton_1());
		}
		return panel;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("연결");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						url = "jdbc:derby://" +server.getText() +"/bookrent;create=true";
						Class.forName(driver);
						textArea.append("네트워크 드라이버가 로딩되었습니다.\n");
						
						System.setProperty("derby.drda.startNetworkServer","true");
						textArea.append("property setting....\n");
						
						
						Properties p = new Properties();
						p.setProperty("user", "bookrent");
						p.setProperty("password", "bookrent");
						
						conn = DriverManager.getConnection(url,p);
						textArea.append("Connection 객체가 생성되었습니다.\n");
						
						conn.commit();
						
							
					}catch(Exception ex){
						textArea.append(ex.toString() + "\n");
						textArea.append(url + "\n");
						
					}
					

				}
			});
		}
		return btnNewButton;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("테이블 생성");
		}
		return btnNewButton_1;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTextArea());
		}
		return scrollPane;
	}
	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
			textArea.setWrapStyleWord(true);
		}
		return textArea;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("원격작업...");
		}
		return lblNewLabel;
	}
	private JTextField getServer() {
		if (server == null) {
			server = new JTextField();
			server.setColumns(10);
		}
		return server;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("SERVER");
		}
		return lblNewLabel_1;
	}
}
